package com.manor.common.utils;

import android.media.MediaPlayer;

import java.io.IOException;


/**

 * @version 1.0

 * @date 2020/6/5  11:01
 */
public class MediaUtil {

    private static MediaUtil sInstance;


    public static MediaUtil getInstance() {
        if (sInstance == null) {
            synchronized (MediaUtil.class) {
                if (sInstance == null) {
                    sInstance = new MediaUtil();
                }
            }
        }
        return sInstance;
    }


    public int getDuration(String audioUrl) {
        MediaPlayer mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(audioUrl);
            mediaPlayer.prepare();
            int duration = mediaPlayer.getDuration();
            if (0 < duration) {
                int s = duration / 1000;
                mediaPlayer.release();
                return s;
            } else {
                mediaPlayer.release();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


}
